import sqlalchemy as sa
from sqlalchemy.orm import relation
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, backref
from sqlalchemy import *
from sqlalchemy import engine, schema
from hellopylons.model import meta

Base = declarative_base()

middle_table = sa.Table('tblmiddle', meta.metadata,
    sa.Column('PK_MiddleID', sa.types.Integer, primary_key=True),
    sa.Column('FK_LeftID', sa.types.Integer, sa.ForeignKey('tblleft.PK_LeftID')),
    sa.Column('FK_RightID', sa.types.Integer, sa.ForeignKey('tblright.PK_RightID')),
)



class Right(Base):
    __tablename__ = 'tblright'
    PK_RightID = sa.Column(sa.types.Integer, primary_key=True)
    Name = sa.Column(sa.types.String)
    
    LeftObj = sa.orm.relation('Left', secondary=middle_table, backref='Right')
    def __init__(self, name):
        self.Name = name
    
class Left(Base):
    __tablename__ = 'tblleft'
    
    PK_LeftID = sa.Column(sa.types.Integer, primary_key=True)
    Name = sa.Column(sa.types.String)
    
    RightObj = sa.orm.relation('Right', secondary=middle_table, backref='Left')
    def __init__(self, name):
        self.Name = name
    

class Middle(Base):
    __tablename__ = 'tblmiddle'
    
    PK_MiddleID = sa.Column(sa.types.Integer, primary_key=True)
    FK_RightID = sa.Column('FK_RightID', sa.types.Integer, sa.ForeignKey('tblright.PK_RightID'))
    FK_LeftID = sa.Column('FK_LeftID', sa.types.Integer, sa.ForeignKey('tblleft.PK_LeftID'))

    